#include "my_inc/main_login.h"
#include "my_inc/photos.h"

// 放大图片
void amg(lv_event_t * e)
{
    static lv_obj_t * temp = NULL;
    img_p img_temp         = lv_event_get_user_data(e);

    // 获取事件代码和目标对象
    lv_event_code_t code   = lv_event_get_code(e);
    lv_obj_t * current_key = lv_event_get_target(e);

    // 只在点击事件时处理按钮逻辑
    if(code == LV_EVENT_CLICKED) {
        // 先处理按钮点击逻辑，再创建界面
        if(current_key != NULL) {
            // 检查是否是右按钮点击（需要提前保存按钮指针）
            if(lv_obj_has_flag(current_key, LV_OBJ_FLAG_USER_1)) { // 右按钮
                img_temp->number = (img_temp->number % 9) + 1;
                printf("右按钮 → 编号: %d\n", img_temp->number);
            } else if(lv_obj_has_flag(current_key, LV_OBJ_FLAG_USER_2)) { // 左按钮
                img_temp->number = (img_temp->number == 1) ? 9 : (img_temp->number - 1);
                printf("左按钮 ← 编号: %d\n", img_temp->number);
            } else if(current_key == img_temp->obj) { // 原始图片点击
                const char * text = lv_label_get_text(img_temp->labell);
                img_temp->number  = atoi(text);
                printf("原始点击 编号: %d\n", img_temp->number);
            }
        }
    }

    // 创建/清空屏幕
    if(temp == NULL) {
        temp = lv_obj_create(NULL);
        lv_obj_set_size(temp, 800, 480);
    } else {
        lv_obj_clean(temp);
    }

    // 创建图片
    char p_name[30];
    sprintf(p_name, "A:img/_%d.png", img_temp->number);
    lv_obj_t * img = lv_img_create(temp);
    lv_img_set_src(img, p_name);
    lv_obj_center(img);
    img_temp->img = img;

    // 设置图片属性
    lv_obj_add_flag(img, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_set_style_bg_opa(img, LV_OPA_0, LV_PART_MAIN);
    lv_obj_set_style_bg_opa(img, LV_OPA_50, LV_PART_MAIN | LV_STATE_PRESSED);
    lv_obj_add_event_cb(img, img_return, LV_EVENT_CLICKED, img_temp);

    // 创建右按钮并标记
    lv_obj_t * obj_right = lv_obj_create(temp);
    lv_obj_set_style_pad_all(obj_right, 0, LV_PART_MAIN);
    lv_obj_set_size(obj_right, 80, 180);
    lv_obj_align(obj_right, LV_ALIGN_RIGHT_MID, 0, 0);
    lv_obj_set_style_bg_opa(obj_right, LV_OPA_30, LV_PART_MAIN);
    lv_obj_add_flag(obj_right, LV_OBJ_FLAG_USER_1); // 标记为右按钮
    lv_obj_add_event_cb(obj_right, amg, LV_EVENT_CLICKED, img_temp);

    // 创建左按钮并标记
    lv_obj_t * obj_left = lv_obj_create(temp);
    lv_obj_set_style_pad_all(obj_left, 0, LV_PART_MAIN);
    lv_obj_set_size(obj_left, 80, 180);
    lv_obj_align(obj_left, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_style_bg_opa(obj_left, LV_OPA_30, LV_PART_MAIN);
    lv_obj_add_flag(obj_left, LV_OBJ_FLAG_USER_2); // 标记为左按钮
    lv_obj_add_event_cb(obj_left, amg, LV_EVENT_CLICKED, img_temp);

    lv_scr_load(temp);
}

// 返回home
void home_pho(lv_event_t * e)
{
    face_p head = lv_event_get_user_data(e);

    // 在链表中查找名为"主界面"的页面节点
    face_p main_face = find(head, "主界面");
    // 安全检查：确保找到主界面且屏幕对象有效
    if(!main_face || !main_face->screen) return;
    // 获取主界面的屏幕对象
    lv_obj_t * scr = main_face->screen;

    // 加载主界面屏幕
    lv_scr_load(scr);
}

// 返回相册
void img_return(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e); // 获取事件类型
    if(code == LV_EVENT_CLICKED) {
        img_p img_temp = lv_event_get_user_data(e);
        lv_scr_load(img_temp->temp->screen);
    }
}

void ref_pho(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        // 获取传递的用户数据（包含容器和 temp）
        ref_data_t * ref_data = lv_event_get_user_data(e);
        if(!ref_data || !ref_data->cont_pho || !ref_data->photo_page) {
            return; // 安全检查：数据无效则退出
        }

        lv_obj_t * cont_pho = ref_data->cont_pho;  // 旧容器
        face_p temp = ref_data->photo_page;        // 直接获取 temp（无需查找）
        lv_obj_t * parent = lv_obj_get_parent(cont_pho); // 父级是 temp->screen

        // 删除旧容器
        lv_obj_del(cont_pho);

        // 创建新容器（复用 photos.c 中的样式）
        static lv_coord_t col_dsc_pho[] = {180, 180, 180, LV_GRID_TEMPLATE_LAST};
        static lv_coord_t row_dsc_pho[] = {180, 180, 180, LV_GRID_TEMPLATE_LAST};
        lv_obj_t * new_cont_pho = lv_obj_create(parent);
        // 复制原容器样式（与 photos.c 中一致）
        lv_obj_set_style_bg_color(new_cont_pho, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
        lv_obj_set_style_bg_opa(new_cont_pho, LV_OPA_0, LV_PART_MAIN);
        lv_obj_set_style_grid_column_dsc_array(new_cont_pho, col_dsc_pho, 0);
        lv_obj_set_style_grid_row_dsc_array(new_cont_pho, row_dsc_pho, 0);
        lv_obj_set_style_pad_column(new_cont_pho, 50, LV_PART_MAIN);
        lv_obj_set_size(new_cont_pho, 720, 480);
        lv_obj_align(new_cont_pho, LV_ALIGN_TOP_LEFT, 0, 0);
        lv_obj_set_style_pad_all(new_cont_pho, 0, LV_PART_MAIN);
        lv_obj_set_layout(new_cont_pho, LV_LAYOUT_GRID);

        // 重建图片网格（修复：使用新容器 new_cont_pho）
        int count = 0;
        for(size_t i = 0; i < 3; i++) {
            for(size_t j = 0; j < 3; j++) {
                lv_obj_t * obj = lv_obj_create(new_cont_pho); // 关键：用新容器
                lv_obj_clear_flag(obj, LV_OBJ_FLAG_SCROLLABLE);
                lv_obj_set_style_border_width(obj, 0, LV_PART_MAIN);
                lv_obj_set_style_bg_opa(obj, LV_OPA_0, LV_PART_MAIN);
                lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, j, 1, LV_GRID_ALIGN_STRETCH, i, 1);

                char p_name[30];
                sprintf(p_name, "A:img/size/_%d.png", ++count);
                if(p_name != NULL) {
                    lv_obj_t * img_obj = lv_img_create(obj);
                    lv_obj_center(img_obj);
                    lv_img_set_src(img_obj, p_name);

                    lv_obj_t * labell = lv_label_create(obj);
                    char num_str[10];
                    snprintf(num_str, sizeof(num_str), "%d", count);
                    lv_label_set_text(labell, num_str);
                    lv_obj_add_flag(labell, LV_OBJ_FLAG_HIDDEN);

                    // 关键：直接使用传递过来的 temp
                    img_p img_temp = malloc(sizeof(img_t));
                    img_temp->temp = temp; // temp 已通过 ref_data 传递，无需查找
                    img_temp->number = atoi(num_str);
                    lv_obj_add_event_cb(obj, amg, LV_EVENT_CLICKED, img_temp);
                } else {
                    lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
                }
            }
        }

        // 更新刷新按钮的用户数据（新容器 + 原 temp）
        lv_obj_t * btn_ref = lv_event_get_target(e);
        free(ref_data); // 释放旧数据
        ref_data_t * new_ref_data = malloc(sizeof(ref_data_t));
        new_ref_data->cont_pho = new_cont_pho;
        new_ref_data->photo_page = temp; // 保持 temp 不变
        lv_obj_remove_event_cb(btn_ref, ref_pho);
        lv_obj_add_event_cb(btn_ref, ref_pho, LV_EVENT_CLICKED, new_ref_data);
    }
}


